package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.service.ISysDeviceService;
import com.ruoyi.system.service.ISysHeartbeartService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备信息Controller
 * 
 * @author ruoyi
 * @date 2021-04-26
 */
@Controller
@RequestMapping("/system/device")
public class SysDeviceController extends BaseController
{
    private String prefix = "system/device";

    @Autowired
    private ISysDeviceService sysDeviceService;
    @Autowired
    private ISysHeartbeartService heartbeartService;
    @RequiresPermissions("system:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }
    boolean ifExpired(Long timeStamp){
        Long currentTime;
        Long lastTime=timeStamp;
        currentTime= System.currentTimeMillis();
        int time1=(int)(currentTime-lastTime)/(1000*60);
        if(time1>=1){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 查询设备信息列表
     */
    @RequiresPermissions("system:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDevice sysDevice)
    {
        startPage();
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        for(int i=0;i<list.size();i++){

            if(ifExpired(list.get(i).getDeviceTimestamp())){
                list.get(i).setDeviceStatus("1");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @RequiresPermissions("system:device:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDevice sysDevice)
    {
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);

        ExcelUtil<SysDevice> util = new ExcelUtil<SysDevice>(SysDevice.class);
        return util.exportExcel(list, "设备信息数据");
    }

    /**
     * 新增设备信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备信息
     */
    @RequiresPermissions("system:device:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDevice sysDevice)
    {
        return toAjax(sysDeviceService.insertSysDevice(sysDevice));
    }

    /**
     * 修改设备信息
     */
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") String deviceId, ModelMap mmap)
    {
        SysDevice sysDevice = sysDeviceService.selectSysDeviceById(deviceId);
        mmap.put("sysDevice", sysDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备信息
     */
    @RequiresPermissions("system:device:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDevice sysDevice)
    {
        return toAjax(sysDeviceService.updateSysDevice(sysDevice));
    }

    /**
     * 删除设备信息
     */
    @RequiresPermissions("system:device:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysDeviceService.deleteSysDeviceByIds(ids));
    }
}
